using System;

using Microsoft.SPOT;

using mfRCF.Core;
using mfRCF.Core.Extensions;

using mfXmlSerializer;

using mfLogger;

using OST.Commands.Extensions;
using OST.Persistence;
using OST.Persistence.Extensions;
using OST.Shared;

namespace OST.Commands
{

    /// <summary>
    /// updates/executes the actual <see cref="DateTime"/> over NTP (network-time-protocol)
    /// </summary>
    public class UpdateNTPGETCommand : mfRcfCommand
    {
        #region CTOR

        public UpdateNTPGETCommand(string httpPath)
            : base(httpPath, new mfGETMethod())
        {
        }

        #endregion

        #region Properties

        /// <summary>
        /// wrapper-property 4 logging
        /// </summary>
        private ILoggerAppender Logger
        {
            get
            {
                return mfLogger.mfLogger.Instance;
            }
        }

        #endregion

        #region mfRcfCommand
        public override void ExecuteCommand(mfIHttpContext httpContext)
        {
            try
            {
                if (!ConfigurationProvider.Instance.HasConfig)
                    throw new OSTException("no config loaded");

                ConfigurationProvider.Instance.ConfigInstance.TimeService.ApplyTimeService();

                var msg = "ntp updated, new DateTime is " + DateTime.Now;
                this.WriteHttpResponseOK(httpContext, msg);

                Logger.Info(msg);
            }
            catch (Exception ex)
            {
                this.WriteHttpResponseError(httpContext, ex);
            }
        }

        #endregion
    }
}
